/**
 * 〈〉
 *
 * @author 王青杰
 * @create 2018-08-0810:20
 * @since 1.0.0
 */
package org.wlgzs.website.wlgzsoffice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wlgzs.website.wlgzsoffice.dao.SweepDao;
import org.wlgzs.website.wlgzsoffice.entity.TbDuty;
import org.wlgzs.website.wlgzsoffice.entity.TbSweep;
import org.wlgzs.website.wlgzsoffice.enums.ResultEnum;
import org.wlgzs.website.wlgzsoffice.service.SweepService;
import org.wlgzs.website.wlgzsoffice.util.ResultMsg;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SweepServiceImpl implements SweepService{

    @Resource
    SweepDao sweepDao;

    @Override
    @Transactional
    public ResultMsg insert(TbSweep sweep) {
        if(sweep!=null){
            sweepDao.insert(sweep);
            return new ResultMsg(ResultEnum.SAVE,sweep);
        }
        return new ResultMsg(ResultEnum.UNSAVE,sweep);
    }

    @Transactional
    @Override
    public ResultMsg delete(int sweepId) {
        if(sweepDao.selectByPrimaryKey(sweepId)!=null){
            sweepDao.deleteByPrimaryKey(sweepId);
            PageHelper.startPage(0,10);
            List<TbSweep> sweepList = sweepDao.checkAll();
            PageInfo pageInfo = new PageInfo(sweepList);
            return new ResultMsg(ResultEnum.DELETE,pageInfo);
        }
        PageHelper.startPage(0,10);
        List<TbSweep> sweepList = sweepDao.checkAll();
        PageInfo pageInfo = new PageInfo(sweepList);
        return new ResultMsg(ResultEnum.UNDELETE,pageInfo);

    }

    @Transactional
    @Override
    public ResultMsg update(TbSweep sweep) {
        if(sweepDao.selectByPrimaryKey(sweep.getSweepId())!=null){
            sweepDao.updateByPrimaryKeySelective(sweep);
            return new ResultMsg(ResultEnum.UPDATE,sweep);
        }
        return new ResultMsg(ResultEnum.UNUPDATE,sweep);
    }

    @Transactional
    @Override
    public ResultMsg checkAll(int pageNum, TbSweep sweep) {
        if(sweep!=null) {
            if (sweep.getSweepDate() == null) {
                sweep.setSweepDate(null);
            }
            if (sweep.getSweepPerson() == null) {
                sweep.setSweepPerson("");
            }
        }
        PageHelper.startPage(pageNum,10);
        List<TbSweep> sweepList = sweepDao.checkAll();
        //List<TbDuty> gradeLis t = dutyDao.checkAll();
        PageInfo pageInfo = new PageInfo(sweepList);
        return new ResultMsg(ResultEnum.QUIRY,pageInfo);
    }
}

